package com.gxa.controller;

import com.gxa.common.R;
import com.gxa.pojo.Emp;
import com.gxa.pojo.EmpEntry;
import com.gxa.service.EmpEntryService;
import com.gxa.utils.GenerateSequenceUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;


@Controller
@RequestMapping("/emp_upload")
@Api(value = "录入员工图片上传" ,tags = "录入员工图片上传接口")
public class UploadFile {
    @Autowired
    private EmpEntryService empEntryService;

    //上传照片
    @RequestMapping(value = "/uploadselfpic",method = RequestMethod.POST)
    @ApiOperation(value = "员工录入",notes = "",httpMethod = "POST", response = Emp.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R upLoadSelfPic(EmpEntry fileUser, HttpServletRequest request) throws IOException {
        R r = new R();
        System.out.println(fileUser.getSelfPic());
//            System.out.println(fileUser.getUsername());
//            System.out.println(fileUser.getPassword());
        if(fileUser.getSelfPic()==null||fileUser.getSelfPic().isEmpty()){
            r.setCode(500);
            r.setMsg("没有选中文件");
        } else{//正常上传
            System.out.println(request.getServletContext().getRealPath(""));
            String uploadPath =  request.getServletContext().getRealPath("") + File.separator + "upload" + File.separator;
            File file = new File(uploadPath);
            if(!file.exists()){
                file.mkdirs();
            }
            MultipartFile multipartFile = fileUser.getSelfPic();
            String fileName = multipartFile.getOriginalFilename();
            String onlyFileName = GenerateSequenceUtil.generateSequenceNo();

            String surrifx = fileName.substring(fileName.lastIndexOf("."));
            String targetFile = onlyFileName+surrifx;
            File newFile = new File(uploadPath + targetFile);
            System.out.println("哈哈newFile=" + targetFile);
            //上传
            FileCopyUtils.copy(multipartFile.getBytes(),newFile);
            String FileName= File.separator + "upload" + File.separator+targetFile;
            fileUser.setEmp_pic(FileName);
            r.setCode(200);
            r.setMsg("成功");
            r.setData(FileName);
        }
        return r;
    }

    //上传照片
    @RequestMapping(value = "/uploadidpic",method = RequestMethod.POST)
    @ApiOperation(value = "员工录入",notes = "",httpMethod = "POST", response = Emp.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R upLoadIdPic(EmpEntry fileUser, HttpServletRequest request) throws IOException {
        R r = new R();
        System.out.println(fileUser.getIdPic());
//            System.out.println(fileUser.getUsername());
//            System.out.println(fileUser.getPassword());
        if(fileUser.getIdPic()==null||fileUser.getIdPic().isEmpty()){
            r.setCode(500);
            r.setMsg("没有选中照片");
        } else{//正常上传
            String uploadPath =  request.getServletContext().getRealPath("")+File.separator + "upload" + File.separator;
            File file = new File(uploadPath);
            if(!file.exists()){
                file.mkdirs();
            }
            MultipartFile multipartFile = fileUser.getIdPic();
            String fileName = multipartFile.getOriginalFilename();
            String onlyFileName = GenerateSequenceUtil.generateSequenceNo();
            String surrifx = fileName.substring(fileName.lastIndexOf("."));
            String targetFile = onlyFileName+surrifx;
            File newFile = new File(uploadPath + targetFile);
            System.out.println("哈哈newFile=" + targetFile);
            //上传
            FileCopyUtils.copy(multipartFile.getBytes(),newFile);
            String FileName= File.separator + "upload" + File.separator+targetFile;
            fileUser.setEmp_pic(FileName);
            r.setCode(200);
            r.setMsg("成功");
            r.setData(FileName);
        }
        return r;
    }
    //上传照片
    @RequestMapping(value = "/uploadentrypic",method = RequestMethod.POST)
    @ApiOperation(value = "员工录入",notes = "",httpMethod = "POST", response = Emp.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R upLoadEntryPic(EmpEntry fileUser, HttpServletRequest request) throws IOException {
        R r = new R();
//            System.out.println(fileUser.getUsername());
//            System.out.println(fileUser.getPassword());
        if(fileUser.getEntryPic()==null||fileUser.getEntryPic().isEmpty()){
            r.setCode(500);
            r.setMsg("没有选中文件");
        } else{//正常上传

            String uploadPath =request.getServletContext().getRealPath("")+  File.separator + "upload" + File.separator;
            File file = new File(uploadPath);
            if(!file.exists()){
                file.mkdirs();
            }
            MultipartFile multipartFile = fileUser.getEntryPic();
            String fileName = multipartFile.getOriginalFilename();
            String onlyFileName = GenerateSequenceUtil.generateSequenceNo();
            String surrifx = fileName.substring(fileName.lastIndexOf("."));
            String targetFile = onlyFileName+surrifx;
            File newFile = new File(uploadPath + targetFile);
            System.out.println("哈哈newFile=" + targetFile);
            //上传
            FileCopyUtils.copy(multipartFile.getBytes(),newFile);
            String FileName= File.separator + "upload" + File.separator+targetFile;
            fileUser.setEmp_pic(FileName);
            r.setCode(200);
            r.setMsg("成功");
            r.setData(FileName);
        }
        return r;
    }


    //上传照片
    @RequestMapping(value = "/uploadsfile",method = RequestMethod.POST)
    @ApiOperation(value = "员工录入",notes = "",httpMethod = "POST", response = EmpEntry.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R upLoadFile(@RequestBody EmpEntry fileUser){
        System.out.println(fileUser.toString());
        R r = new R();
        int result= empEntryService.empEntry(fileUser);
        r.setCode(200);
        r.setMsg("success");
        return r;
    }
}
